package com.huluobo.a2303mdmdemo.pages

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.huluobo.a2303mdmdemo.MyNavHost
import com.huluobo.a2303mdmdemo.R
import com.huluobo.a2303mdmdemo.demo.FirstPage
import com.huluobo.apps.pages.AppsPage
import com.huluobo.document.pages.LocalFilePage
import com.huluobo.lib_base.RoutePath
import com.huluobo.report.pages.ReportPage

/**
 *  Created by LC on 2025/8/19.
 */

//封装底部导航的item
sealed class NavItem(val route: String, val title: String, val img: Int) {
    data object MessagePage : NavItem(RoutePath.MESSAGE, "消息", R.drawable.message)
    data object HomePage : NavItem(RoutePath.HOME, "门户", R.drawable.home)
    data object MinePage : NavItem(RoutePath.MINE, "我的", R.drawable.mine)
}

@Composable
fun MainPage() {
    //底部导航的数据源
    val list = listOf(NavItem.MessagePage, NavItem.HomePage, NavItem.MinePage)

    //导航控制器
    val navHostController = rememberNavController()

    //童工导航控制器拿到回退站并转换为一个状态
    val stackEntryState by navHostController.currentBackStackEntryAsState()

    //通过回退站拿到当前导航中所有的路由地址
    val destination = stackEntryState?.destination

    Scaffold(bottomBar = {
        if (destination?.hierarchy?.any {
                //通过拿到底部导航的地址(历史记录)来对比,如果其中有底部导航数据源的3个页面地址的话,就为true,展示底部导航
                list.map { navItem ->
                    //list.map 遍历集合,拿到所有地址的部分,转化
                    navItem.route
                    //contains 包含,如果遍历之后的地址包含回退站中的地址,就为true,显示底部导航
                }.contains(it.route)
            } == true) {
            //导航栏
            NavigationBar {
                //遍历数据源,产生每个item
                list.forEach {
                    //导航栏的item
                    NavigationBarItem(selected = false, onClick = {
                        navHostController.navigate(it.route)
                    }, icon = {//图标
                        Icon(
                            painter = painterResource(id = it.img),
                            contentDescription = "",
                            modifier = Modifier.size(30.dp)
                        )
                    }, label = {//标题
                        Text(text = it.title)
                    })
                }
            }
        }


    }) {
        MyNavHost(it = it, navHostController = navHostController)
    }
}